<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>礼品详情</title>
	<link rel="stylesheet" href="__HCSS__/gift.css">
	<link rel="stylesheet" href="__HCSS__/message.css">
	<script src="__HJS__/jquery-3.3.1.min.js"></script>
	<script src="__HJS__/message.min.js"></script>
</head>
<body>
	<div class="gift-wrap">
		<div class="gift-main">

			<!-- 页面位置-->
			<div class="giftDetailTop">
				<div class="title">
					<span></span>
					<a href="">所有礼物</a>/
					<p>爱奇艺黄金会员</p>
				</div>
			</div>
			<div class="giftDetailBox">
					<div class="giftDetailImg">
						<img src="/uploads/goodsimg/">
					</div>

					<div class="giftDetailInfo">
						<h4>{$res.goodsName}</h4>
						<p>
							{$res.description}
							<a href="" >了解更多</a>
						</p>

						<div class="giftPrice">
							<p>市场价 &nbsp;&nbsp;&nbsp; <s></s></p>
							<p>
								<span>
									促销价 &nbsp;&nbsp;&nbsp; <em id="price"></em>
								</span>
							</p>
						</div>

					<div class="giftTypeBox">
						<span>选择面值</span>
						{volist name="data" id="v"}
						<div class="giftType" proid="{$v.id}">{$v.property}</div>
						{/volist}
					</div>

					<div class="buyGiftBtn">立即采购</div>
					<p class="tipsText">温馨提示：以上价格含税，可开具发票；礼品不支持退换</p>
				</div>
			</div>



		</div>
	</div>

	<script>
		$(document).ready(function(){

			// 商品id
			var gid = {$res.id};

			// 礼物中心物品类型选项js
            i = 0;
            var proid = $('.giftTypeBox>.giftType').eq(i).attr('proid');
            readAjax(proid);

            $('.giftTypeBox>.giftType').eq(0).addClass('giftTypeClick');
			$('.giftTypeBox>.giftType').click(function(){
                $(this).addClass('giftTypeClick').siblings().removeClass('giftTypeClick');
                var proid = $(this).attr('proid');
                i = $(this).index() - 1;
                readAjax(proid);
            });


			// 购买弹窗
			$('.buyGiftBtn').click(function(){
                var proid = $('.giftTypeBox>.giftType').eq(i).attr('proid');
				$('.backDrop', window.parent.document).show();
				$('.popBoxForBuy',window.parent.document).show();
				buyAjax(proid);
            });

			//增加购买数量
			var num = 1;
			$(window.parent.document).on('click','.addNum',function(){
				num++;
			    var price = $('.price',window.parent.document).html();
				$('.nowNum',window.parent.document).val(num);
   				$('.total',window.parent.document).html(num * price + '.00');
			});

            //减去购买数量
            $(window.parent.document).on('click','.subNum',function(){
                num--;
                var price = parseInt($('.price',window.parent.document).html());
                if(num <= 0){
                    num = 1;
				}
                $('.nowNum',window.parent.document).val(num);
                $('.total',window.parent.document).html(num * price + '.00');

            });

			// 购买弹窗关闭
			$('.closeGiftBox', window.parent.document).click(function(){
				$('.backDrop', window.parent.document).hide();
				$('.popBoxForBuy', window.parent.document).hide();
			});

			//结算
			// console.log($('.settlementBtn'));
            $('.settlementBtn', window.parent.document).click(function(){
                var proid = $('.giftTypeBox>.giftType').eq(i).attr('proid');
                var count = parseInt($('.nowNum',window.parent.document).val());
                var orderMoney = parseInt($('.total',window.parent.document).html());
                settleAjax(gid,proid,count,orderMoney);
            });


		});


        // 礼物中心物品类型选项ajax
		function readAjax(proid){
			$.ajax({
				type : 'get',
				url : '/home/goods/profind/proid/' + proid,
				dataType : 'json',
                success: function (data) {
                    if (data.status) {
                        $('.giftDetailImg img').attr('src','/uploads/goodsimg/' + data.proImg);
                        $('.giftPrice p').eq(0).children('s').html('￥'+ data.marketPrice);
                        $('.giftPrice #price').html('￥' + data.price);
                        return;
                    } else {
                        alert('暂时缺货');
                        return;
                    }
                },
                error: function (data) {
                    alert('AJAX执行失败!');
                }
			});
		}

        // 购买弹窗ajax
        function buyAjax(proid){
			$.ajax({
				type : 'get',
				url : '/home/goods/profind/proid/' + proid,
				dataType : 'json',
				success : function(data){
				    if(data['status']){
						$('.price',window.parent.document).html(data['price']);
                        $('.giftText_small',window.parent.document).html( data['proName'] );
                        $('.total',window.parent.document).html( data['price'] );
						return;
                    }else{
                        alert('暂时缺货');
                        return;
					}
				},
				error : function(){
                    alert('AJAX执行失败!');
				}

			});
        }

        //结算ajax
        function settleAjax(gid,proid,count,orderMoney){
			$.ajax({
				type : 'post',
				url : '/home/goods/settle/gid/' + gid,
                data : {'proid':proid,'count':count,'orderMoney':orderMoney},
				dataType : 'json',
				success : function(data){
					if(data.status){
						$('.backDrop', window.parent.document).hide();
						$('.popBoxForBuy', window.parent.document).hide();
						$('.nowNum',window.parent.document).val(1);
						$.message(data.info);
					}else{
                        $.message({
	                        message: data.info,
	                        type:'error'
	                    });
					}
				},
				error : function(data){
                    console.log(data);
                    alert('执行失败');
				}
			});
		}
	</script>

</body>
</html>